<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2>登录</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginForm" @submit.native.prevent="handleLogin">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码"></el-input>
        </el-form-item>
        <el-button type="primary" native-type="submit" style="width: 100%">登录</el-button>
        <div class="register-link">
          <router-link to="/register">没有账号？立即注册</router-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '用户名长度在 3 到 15 个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          const adminUser = this.$store.state.adminUser
          if (this.loginForm.username === adminUser.username && 
              this.loginForm.password === adminUser.password) {
            this.$store.commit('setUser', { ...adminUser })
            this.$router.push('/admin')
          } else {
            this.$store.commit('setUser', { username: this.loginForm.username })
            this.$router.push('/menu')
          }
          this.$message.success('登录成功')
        } else {
          this.$message.error('请输入完整的用户名和密码')
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #1a1c20 0%, #2c3e50 100%);
}
.login-card {
  width: 400px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 20px;
}
.login-card h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
  font-size: 28px;
}
.register-link {
  margin-top: 15px;
  text-align: center;
}
.register-link a {
  color: #409EFF;
  text-decoration: none;
  transition: all 0.3s ease;
}
.register-link a:hover {
  color: #66b1ff;
}
.el-form-item {
  margin-bottom: 25px;
}
</style>
